<?php
// +----------------------------------------------------------------------
// | RXThinkCMF_EVTP5.1_PRO前后端分离旗舰版框架 [ RXThinkCMF ]
// +----------------------------------------------------------------------
// | 版权所有 2018~2021 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.rxthink.cn
// +----------------------------------------------------------------------
// | 作者: ZhangXuanNeng <rxthinkcmf@163.com>
// +----------------------------------------------------------------------
// | 免责声明:
// | 本软件框架禁止任何单位和个人用于任何违法、侵害他人合法利益等恶意的行为，禁止用于任何违
// | 反我国法律法规的一切平台研发，任何单位和个人使用本软件框架用于产品研发而产生的任何意外
// | 、疏忽、合约毁坏、诽谤、版权或知识产权侵犯及其造成的损失 (包括但不限于直接、间接、附带
// | 或衍生的损失等)，本团队不承担任何法律责任。本软件框架只能用于公司和个人内部的法律所允
// | 许的合法合规的软件产品研发，详细声明内容请阅读《框架免责声明》附件；
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\admin\service\SettlementStatementService;
use app\common\controller\Export;
use think\Db;

/**
 * 对账单单-控制器
 * @author ZhangXuanNeng
 * @since 2024/01/20
 * Class Settlementstatement
 * @package app\admin\controller
 */
class Settlementstatement extends Backend
{
    /**
     * 初始化
     * @author ZhangXuanNeng
     * @since 2020/11/19
     */
    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->service = new SettlementStatementService();
    }
    // 打款
    public function setStatus(){
        $result = $this->service->setStatus();
        return $this->jsonReturn($result);
    }
    // 撤回
    public function setRevoke(){
        $result = $this->service->setRevoke();
        return $this->jsonReturn($result);
    }
    // 导出
    public function export(){
        $param = request()->param();
        // 查询条件
        $map = [];
        if(isset($param['promote_1_id']) && !empty($param['promote_1_id'])){
            $map[] = ['promote_1_id','=',$param['promote_1_id']];
        }
        if(isset($param['promote_id']) && !empty($param['promote_id'])){
            $map[] = ['promote_id','=',$param['promote_id']];
        }
        if(isset($param['settlement_number']) && !empty($param['settlement_number'])){
            $map[] = ['settlement_number','=',$param['settlement_number']];
        }
        if(isset($param['status']) && !empty($param['status'])){
            $map[] = ['status','=',$param['status']];
        }
        if(isset($param['s_account_name']) && !empty($param['s_account_name'])){
            $map[] = ['s_account_name','like','%'.$param['s_account_name'].'%'];
        }
        if(isset($param['create_time']) && !empty($param['create_time'])){
            $map[] = ['create_time','between time',$param['create_time']];
        }
        $Export = new Export();
        $Export->expUser(5,$map);
    }
    // 下载
    public function downStatement(){
        $param = request()->param();
        $withdraw_info = Db::name('settlement_statement')->where(['id'=>$param['id']])->find();
        $payment_info = Db::name('settlement_account')->where(['id'=>$withdraw_info['payment_id']])->find();
        $settlement_info = Db::name('settlement')->where(['id'=>$withdraw_info['settlement_id']])->find();
        if($settlement_info['violation_money'] == 0){
            $settlement_info['violation_remark'] = '';
        }
        $details = Db::name('settlement_details')->where(['settlement_id'=>$withdraw_info['settlement_id']])->select();
        $sum_money = Db::name('settlement_details')->where(['settlement_id'=>$withdraw_info['settlement_id']])->sum('sum_money');
        $dif_money = $settlement_info['event_money'] == 0 ? $settlement_info['sum_money'] - $sum_money : $settlement_info['event_money'];
        $sum_money_capital = convertAmountToCn($settlement_info['sum_money']);
        $str = '';
        $str .= '<!DOCTYPE html>
                    <html lang="en">
                        <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>结算对账单</title>
    <style>
        /* CSS Document */
        /*!
         * @名称：base.css
         * @功能：1、重设浏览器默认样式
         *       2、设置通用原子类
         */
        /* 防止用户自定义背景颜色对网页的影响，添加让用户可以自定义字体 */
        *{
            box-sizing: border-box;
        }
        html {
            background:#f2f2f2;
            color:#000;
        }
        /* 内外边距通常让各个浏览器样式的表现位置不同 */
        body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,textarea,p,blockquote,th,td,hr,button,article,aside,details,figcaption,figure,footer,header,hgroup,menu,nav,section {
            margin:0;
            padding:0;
        }
        html, body {
            width: 100%;
            height: 100%
        }
        html {
            position: relative;
        }
        body {
            position: absolute;
            left: 0;
            top: 0;
            background-color: #fff;
        }
        table {
            border-collapse:collapse;
            border-spacing:0;
        }
        th {
            text-align:inherit;
        }
        fieldset,img {
            border:none;
        }
        iframe {
            display:block;
        }
        abbr,acronym {
            border:none;
            font-variant:normal;
        }
        del {
            text-decoration:line-through;
        }
        address,caption,cite,code,dfn,em,th,var {
            font-style:normal;
            font-weight:500;
        }
        ol,ul {
            list-style:none;
        }
        caption,th {
            text-align:left;
        }
        h1,h2,h3,h4,h5,h6 {
            font-size:100%;
            font-weight:500;
        }
        q:before,q:after {
            content:\'\';
        }
        sub,sup {
            font-size:75%;
            line-height:0;
            position:relative;
            vertical-align:baseline;
        }
        sup {
            top:-0.5em;
        }
        sub {
            bottom:-0.25em;
        }
        a:hover {
            text-decoration:underline;
        }
        ins,a {
            text-decoration:none;
        }
        a:focus,*:focus {
            outline:none;
        }
        .clearfix:before,.clearfix:after {
            content:"";
            display:table;
        }
        .clearfix:after {
            clear:both;
            overflow:hidden;
        }
        .clearfix {
            zoom:1;
        }
        .clear {
            clear:both;
            display:block;
            font-size:0;
            height:0;
            line-height:0;
            overflow:hidden;
        }
        .hide {
            display:none;
        }
        .block {
            display:block;
        }
        .fl,.fr {
            display:inline;
        }
        .fl {
            float:left;
        }
        .fr {
            float:right;
        }
        #account_form {
            min-width: 800px;
            max-width: 800px;
            margin: auto;
            font-size: 15.4px;
        }
        #account_form th {
            width: 800px;
            height: 40px;
            font-size: 20px;
            font-weight: 700;
            text-align: center;
            background-color: #c9c9c9;
            opacity: 0.6;
        }
        #account_form td {
            height: 40px;
            text-align: center;
        }
        #account_form td .remark {
            text-align: left;
            display: inline-block;
            padding: 12px 6px;
            word-break: break-all;
            max-width: 800px;
        }
        #account_form .text_right {
            text-align: end;
            padding-right: 2px;
        }
        #account_form .red {
            color: red;
        }
        #account_form .text_right_bold {
            font-weight: 700;
            text-align: end;
            padding-right: 2px;
        }
        #account_form .text_left_bold {
            font-weight: 700;
            text-align: start;
            padding-left: 6px;
        }
        #account_form .text_left td {
            text-align: start;
            padding-left: 6px;
        }
        #account_form .title {
            height: 40px;
            text-align: center;
            font-size: 18px;
            font-weight: 700;

        }
        #account_form .title_two td {
            font-weight: 700;
            background-color: #c9c9c9;
        }
        #account_form .title_one {
            font-weight: 700;
        }
        #account_form .title_one td:nth-child(1) {
            width: 170px;
        }
        #account_form .title_one td:nth-child(2) {
            width: 140px;
        }
        #account_form .title_one td:nth-child(3) {
            width: 110px;
        }
        #account_form .title_one td:nth-child(4) {
            width: 120px;
        }
        #account_form .table_end td {
            font-size: 14px;
            padding: 12px 6px;
            text-align: start;
            color: red;
            font-weight: 700;
        }

    </style>
</head>
                        <body>
                        <table id="account_form"  border="1" cellspacing="0">
                            <tbody>
                            <tr><th  colspan="6">结算对账单</th></tr>
                            <tr class="title"><td  colspan="6" style="background-color: #c9c9c9">'.$settlement_info['settlement_number'].'</td></tr>
                            <tr class="title_one">
                                <td>结算周期</td>
                                <td>后台充值</td>
                                <td>比例</td>
                                <td>预结算总计</td>
                                <td> 游戏ID</td>
                                <td>渠道ID</td>
                            </tr>';
        foreach ($details as $k=>$v){
            $str .= '<tr>
                            <td>'.date('Y-m-d',$v['starttime']).'至'.date('Y-m-d',$v['endtime']).'</td>
                            <td class="text_right"><span class="fr">&yen;'.$v['total_money'].'</span></td>
                            <td class="red" style="border-color: #000">'.$v['proportion'].'%</td>
                            <td class="text_right"><span class="fr">&yen;'.$v['sum_money'].'</span></td>
                            <td>'.$v['package_id'].'</td>
                            <td>'.$v['promote_id'].'</td>
                        </tr>';
        }
        $str .= '<tr><td  colspan="6">异常事件金额:'.$dif_money.'</td></tr>
                        <tr><td  colspan="6"><div class="remark">异常事件原因:&nbsp;'.$settlement_info['remark'].'</div></td></tr>
                        <tr><td  colspan="6">违规事件金额:'.$settlement_info['violation_money'].'</td></tr>
                        <tr><td  colspan="6"><div class="remark">违规事件原因:&nbsp;'.$settlement_info['violation_remark'].'</div></td></tr>
                        <tr>
                            <td class="text_right_bold" colspan="3"><span class="fr">结算总计（小写）</span></td>
                            <td class="text_left_bold" colspan="3"><span class="fl">&yen;'.$settlement_info['sum_money'].'</span></td>
                        </tr>
                        <tr>
                            <td class="text_right_bold" colspan="3"><span class="fr">结算总计（大写）</span></td>
                            <td class="text_left_bold" colspan="3"><span class="fl">'.$sum_money_capital.'</span></td>
                        </tr>
                        <tr class="text_left title_two">
                            <td colspan="1" style="background-color: #c9c9c9"><span class="fl">付款方</span></td>
                            <td colspan="2" style="background-color: #c9c9c9"></td>
                            <td colspan="1" style="background-color: #c9c9c9"><span class="fl">收款方</span></td>
                            <td colspan="2" style="background-color: #c9c9c9"></td>
                        </tr>
                        <tr class="text_left">
                            <td colspan="1"><span class="fl">银行账户名称</span></td>
                            <td colspan="2"><span class="fl">'.$payment_info['account_name'].'</span></td>
                            <td colspan="1"><span class="fl">银行账户名称</span></td>
                            <td colspan="2"><span class="fl">'.$withdraw_info['s_account_name'].'</span></td>
                        </tr>
                        <tr class="text_left">
                            <td colspan="1"><span class="fl">银行账号</span></td>
                            <td colspan="2"><span class="fl"> \''.$payment_info['account'].'</span></td>
                            <td colspan="1"><span class="fl">银行账号</span></td>
                            <td colspan="2"><span class="fl">\''.$withdraw_info['s_account'].'</span></td>
                        </tr>
                        <tr class="text_left">
                            <td colspan="1"><span class="fl">开户银行</span></td>
                            <td colspan="2"><span class="fl">'.$payment_info['bank_account'].'</span></td>
                            <td colspan="1"><span class="fl">开户银行</span></td>
                            <td colspan="2"><span class="fl">'.$withdraw_info['s_bank_account'].'</span></td>
                        </tr>
                        <tr class="text_left">
                            <td colspan="1"><span class="fl">联系人</span></td>
                            <td colspan="2"><span class="fl">'.$payment_info['user_name'].'</span></td>
                            <td colspan="1"><span class="fl">联系人</span></td>
                            <td colspan="2"><span class="fl">'.$withdraw_info['s_user_name'].'</span></td>
                        </tr>
                        <tr class="text_left">
                            <td colspan="1"><span class="fl">联系电话</span></td>
                            <td colspan="2"><span class="fl">'.$payment_info['phone'].'</span></td>
                            <td colspan="1"><span class="fl">联系电话</span></td>
                            <td colspan="2"><span class="fl">'.$withdraw_info['s_phone'].'</span></td>
                        </tr>
                        <tr class="table_end">
                            <td colspan="6" style="height: 100px;">注：1.收到对帐单后请确认数据，金额及账户信息，并打印盖上公司公章或按手印，一式两份一起邮寄。<br/>
                                2.本对账单作为甲乙双方于   年   月   日已签署的《CPS合作运营协议》附件，本对账单未约定部分以《CPS合作运营协议》约定为准。
                            </td>
                        </tr>
                        </tbody>
                    
                    </table>
                    </body>
                    </html>';
        $fileName = date('Y-m-d',time()).'-'.$withdraw_info['promote_account'];
        exportTxt($fileName,$str,'zip');
        $name = \Env::get('root_path')."public/uploads/zip/".$fileName.'.txt';
        $newName = \Env::get('root_path')."public/uploads/zip/".$fileName.'.xls';
        rename($name,$newName);
        ob_clean();
        flush();
        header( "Content-Type: application/vnd.ms-excel; name='excel'" );
        header("Content-Transfer-Encoding: Binary");
        header("Content-Length: " . filesize($newName));
        header("Content-Disposition: attachment; filename=\"" . basename($newName) . "\"");
        ob_clean();
        flush();
        readfile($newName);
        @unlink ($newName);
        exit;
    }
}